/**
 * 
 */
package com.lxp.ssh2.vo;

import java.util.HashMap;
import java.util.Map;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

/**
 * @Package com.lxp.ssh2.vo
 * @Class com.lxp.ssh2.vo.PageRequest
 * @Description: 封装分页请求信息
 * @Author LiXiaoPeng
 * @Date 2015年4月21日 下午6:40:21
 * @Version V1.0
 * Copyright  Corporation 2015
 */
public class PageRequest {
	private Integer index;
	private Integer size;
	private Map<String, Object> condition;
	
	
	public static final String PARAM_INDEX="index";
	public static final String PARAM_SIZE="size";
	public static final String PARAM_Q="q";
	
	public Integer getIndex() {
		return index;
	}
	public void setIndex(Integer index) {
		this.index = index;
	}
	public Integer getSize() {
		return size;
	}
	public void setSize(Integer size) {
		this.size = size;
	}

	public Map<String, Object> getCondition() {
		return condition;
	}
	public void setCondition(Map<String, Object> condition) {
		this.condition = condition;
	}
	public PageRequest() {
	}
	public PageRequest(Integer index, Integer size) {
		super();
		this.index = index;
		this.size = size;
	}
	
	public Integer getFirstRequest() {
		return (index-1)*size;
	}
	
	
	public Map<String, String> convertCondition() {
		if(CollectionUtils.isEmpty(condition)) {
			return null;
		}
		Map<String, String> result = new HashMap<String, String>();
		for (Map.Entry<String, Object> entry : condition.entrySet()) {
			String key = entry.getKey();
			if(StringUtils.startsWithIgnoreCase(key,PARAM_Q+".") && !StringUtils.isEmpty(entry.getValue())){
				String field=key.substring((PARAM_Q+".").length());
				String value=entry.getValue().toString();
				result.put(field, value);
			}
		}
		return result;
	}
}
